import { defineStore } from 'pinia';

export interface IPieceInfo {
  x: number;
  y: number;
  c: number; // 类型
  fixed: boolean; // 是否可移动
  isArrived?: boolean; // 途经点或终点是否到达
}

interface IOperation {
 [props:string]: any;
}

type IPointPosArr = [number, number];

const ENUM_ELEM_TYPE = {
  KING_WHITE: 0, // 白王棋
  KING_BLACK: 1, // 黑王棋
  PAWN_WHITE: 2, // 白兵棋
  PAWN_BLACK: 3, // 黑兵棋
  PASSPOINT: 4, // 途经点
  TERMINAL: 5, // 终点
};

// 根据起点和终点计算连接起点和终点的斜线路径上的所有点
function calcPathPoints(startPos: IPointPosArr, endPos: IPointPosArr) {
  const xGap = endPos[0] - startPos[0];
  const yGap = endPos[1] - startPos[1];
  const absXGap = Math.abs(xGap);
  const absYGap = Math.abs(yGap);
  const xStep = xGap / absXGap;
  const yStep = yGap / absYGap;
  const points: Array<IPointPosArr> = [];
  for (let i = 0; i <= absXGap; i += 1) {
    points.push([startPos[0] + (i * xStep), startPos[1] + (i * yStep)]);
  }
  return points;
}

export function isMovablePiece(pieceInfo: IPieceInfo) {
  if (pieceInfo.fixed) return false;
  if (pieceInfo.c === ENUM_ELEM_TYPE.PASSPOINT || pieceInfo.c === ENUM_ELEM_TYPE.TERMINAL) return false;
  return true;
}

export function isPawnPiece(pieceInfo: IPieceInfo) {
  if (pieceInfo.c === ENUM_ELEM_TYPE.PAWN_BLACK || pieceInfo.c === ENUM_ELEM_TYPE.PAWN_WHITE) return true;
  return false;
}

export function createStore(pageNumber: number, widgetId: string) {
  return defineStore(`p${pageNumber}_${widgetId}`, {
    state() {
      return {
        mode: 'pawn',
        preset:{}as any,
  
        fullPath: '',
        initialPreset: {} as any,
        operations: [] as any,
        setBackNumber:1,
      };
    },
    getters: {
      isMissionCompleted(state): boolean {
        for (let i = 0; i < state.preset.length; i++) {
          if (state.preset[i].c === ENUM_ELEM_TYPE.PASSPOINT && !state.preset[i].isArrived) return false;
          if (state.preset[i].c === ENUM_ELEM_TYPE.TERMINAL && !state.preset[i].isArrived) return false;
        }
        return true;
      }
    },
    actions: {
      setFullPath(value: string) {
        // 去除掉结尾的/
        this.fullPath = value.replace(/\/+$/, '');
      },
      reset() {
        this.preset = JSON.parse(JSON.stringify(this.initialPreset));
        this.operations = [];
      },
      setPreset(preset: Array<IPieceInfo>) {
        this.preset = preset;
        this.initialPreset = JSON.parse(JSON.stringify(preset));
      },
      setMode(mode: string) {
        this.mode = mode;
      },
      setBack(){
        this.setBackNumber ++
      },
      setOperations (val){
        this.operations = val;
      },
      updatePiecePos(pieceIndex: number, x: number, y: number) {
        const pieceInfo = this.preset[pieceIndex];
        if (pieceInfo) {
          const startPos: IPointPosArr = [pieceInfo.x, pieceInfo.y];
          const endPos: IPointPosArr = [x, y];
          const pathPoints = calcPathPoints(startPos, endPos);
          this.preset.forEach((p) => {
            if (p.c === ENUM_ELEM_TYPE.PASSPOINT) {
              for (let i = 0; i < pathPoints.length; i++) {
                if (pathPoints[i][0] === p.x && pathPoints[i][1] === p.y) {
                  // eslint-disable-next-line no-param-reassign
                  p.isArrived = true;
                  break;
                }
              }
            }
            if (p.c === ENUM_ELEM_TYPE.TERMINAL && x === p.x && y === p.y) {
              // eslint-disable-next-line no-param-reassign
              p.isArrived = true;
            }
          });
          pieceInfo.x = x;
          pieceInfo.y = y;
          this.operations.push({
            type: 'move',
            data: { index: pieceIndex, x, y },
            time: Date.now(),
          });
        }
      }
    }
  })
}
